/*
Copyright (c) 2010 Tomas Lindquist Olsen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <cassert>
#include <cstdio>
#include <cstring>
#include <fltk/SharedImage.h>
#include <fltk/Widget.h>
#include <fltk/Window.h>
#include <fltk/run.h>
#include <fltk/ask.h>
#include <fltk/x11.h>

#include <lua.hpp>
#include <tolua++.h>

#include "gui.h"

int gui_wait_for_all(int argc, const char** argv)
{
    // run until all fltk windows are gone!
    while (fltk::Window::first()) fltk::wait();
}

void gui_default_callback(fltk::Widget* widget, void* data)
{
    lua_State* L = (lua_State*)data;
    //printf("the default callback was executed\n");

    lua_pushlightuserdata(L, (void*)widget);
    lua_rawget(L, LUA_REGISTRYINDEX);
    if (lua_isnil(L, -1))
        fprintf(stderr, "widget not in callback registry");

    tolua_pushusertype(L,(void*)widget,"Widget");
    if (lua_pcall(L, 1, 0, 0) != 0)
    {
        fprintf(stderr, "error in callback: %s\n", lua_tostring(L, -1));
    }
}

/* method: gui_callback_wrapper_lua of class  Widget */
int tolua_binding_Widget_callback00(lua_State* L)
{
    tolua_Error tolua_err;
    if (!tolua_isusertype(L,1,"Widget",0,&tolua_err))
        goto tolua_lerror;
    else
    {
        fltk::Widget* self = (fltk::Widget*)  tolua_tousertype(L,1,0);
        if (!self)
            tolua_error(L,"invalid 'self' in function 'Widget.callback'", NULL);
        {
            assert(lua_isfunction(L, 2) && "can only register function as callback");
            lua_pushlightuserdata(L, (void*)self);
            lua_insert(L, 2);
            lua_rawset(L, LUA_REGISTRYINDEX);
            self->callback(gui_default_callback, (void*)L);
        }
    }
    return 0;

tolua_lerror:
    tolua_error(L,"#ferror in function 'callback'.",&tolua_err);
    return 0;
}

void gui_message(const char* s)
{
    fltk::message("%s", s);
}
void gui_alert(const char* s)
{
    fltk::alert("%s", s);
}
int gui_ask(const char* s)
{
    fltk::ask("%s", s);
}

#include <X11/Xlib.h>

void gui_apply_wm_hint(fltk::Window* wnd, const char* hint, int action)
{
    //assert(CreatedWindow::find(wnd) != 0 && "window not ready to receive properties yet");

//     printf("wm state hinting Window(%p) %s %s\n",
//         wnd,
//         action == HINT_REMOVE ? "remove" : action == HINT_ADD ? "add" : "toggle",
//         hint);

    Display *d = fltk::xdisplay;
    XWindow w = fltk::xid(wnd);

    Atom p = XInternAtom(d, "_NET_WM_STATE", False);
    Atom h = XInternAtom(d, hint, False);

    XEvent xev;
    memset(&xev, 0, sizeof(xev));

    // http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html#id2507241
    xev.type = ClientMessage;
    xev.xclient.window = w;
    xev.xclient.message_type = p;
    xev.xclient.format = 32;
    xev.xclient.data.l[0] = action;
    xev.xclient.data.l[1] = h;

    XSendEvent(d, DefaultRootWindow(d), False, PropertyChangeMask, &xev);
    //XFlush(d);
}

const char* SKIP_TASKBAR_HINT   = "_NET_WM_STATE_SKIP_TASKBAR";
const char* STICKY_HINT         = "_NET_WM_STATE_STICKY";
const char* ABOVE_HINT          = "_NET_WM_STATE_ABOVE";
const char* BELOW_HINT          = "_NET_WM_STATE_BELOW";
